home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VGATUT1.ZIP / VGAPIXEL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  4.4 KB  |  174 lines

  1. //--------------------------------------------
  2. // PIXELS - Barny Mercer 29/7/95 @ 1:48am
  3. //
  4. // Demonstrates 3 different methods of writing
  5. // pixels to a VGA screen.
  6. //--------------------------------------------
  7.  
  8. #include <conio.h>
  9. #include <stdio.h>
  10. #include <memory.h>
  11. #include <stdlib.h>
  12.  
  13. #define VID_320x200    0x0013
  14. #define VID_TEXT        0x0003
  15.  
  16. void VidMode( int Mode );
  17. void PutINTPixel( int X, int Y, unsigned char Colour );
  18. void PutDMAPixel( int X, int Y, unsigned char Colour );
  19. void PutASMPixel( int X, int Y, unsigned char Colour );
  20. void Intro(void);
  21. void Outtro(void);
  22.  
  23. unsigned char *vga = (unsigned char *)0xA0000000;
  24.  
  25. void main(void)
  26. {
  27.     // make sure we're in text mode first for intro message
  28.     VidMode( VID_TEXT );
  29.  
  30.     Intro();
  31.  
  32.         // select video mode
  33.         VidMode( VID_320x200 );
  34.  
  35.     printf( "Interupt Pixels..." );
  36.     getch();
  37.  
  38.     for (int loops=0; loops<9; loops++)
  39.     {
  40.         for (int Tmp=0; Tmp<200; Tmp++)
  41.         {
  42.          for (int Tmp2=0; Tmp2<320; Tmp2++)
  43.              PutINTPixel( Tmp2, Tmp, rand() );
  44.         }
  45.     }
  46.  
  47.     getch();
  48.  
  49.     // use video mode to clear screen
  50.     VidMode( VID_320x200 );
  51.     printf( "DMA Pixels....." );
  52.     getch();
  53.  
  54.     for (loops=0; loops<9; loops++)
  55.     {
  56.         for (int Tmp=0; Tmp<200; Tmp++)
  57.         {
  58.          for (int Tmp2=0; Tmp2<320; Tmp2++)
  59.              PutDMAPixel( Tmp2, Tmp, rand() );
  60.         }
  61.     }
  62.  
  63.     VidMode( VID_320x200 );
  64.     printf( "Assembly Pixels....." );
  65.     getch();
  66.  
  67.     for (loops=0; loops<9; loops++)
  68.     {
  69.         for (int Tmp=0; Tmp<200; Tmp++)
  70.         {
  71.          for (int Tmp2=0; Tmp2<320; Tmp2++)
  72.              PutASMPixel( Tmp2, Tmp, rand() );
  73.         }
  74.     }
  75.  
  76.         getch();        // wait for keypress
  77.  
  78.         // return to text mode
  79.     VidMode( VID_TEXT );
  80.  
  81.     Outtro();
  82. }
  83.  
  84. //------------------------------------------------------
  85. // VidMode - Uses BIOS to switch to video mode specified
  86. //------------------------------------------------------
  87.  
  88. void VidMode( int Mode )
  89. {
  90.         _asm
  91.         {
  92.                 mov ax, [Mode]
  93.                 int 10h
  94.         }
  95. }
  96.  
  97. //---------------------------------------------------------
  98. // PutDMAPixel - Uses Direct Memory Access to place a pixel
  99. //               at X, Y
  100. //---------------------------------------------------------
  101.  
  102. void PutDMAPixel( int X, int Y, unsigned char Colour )
  103. {
  104.     memset(vga+(Y*320)+X, Colour, 1);    // on-the-fly version
  105. }
  106.  
  107. //------------------------------------------------------
  108. // PutINTPixel - Uses BIOS interrupts to place a pixel
  109. //               at X, Y
  110. //------------------------------------------------------
  111.  
  112. void PutINTPixel( int X, int Y, unsigned char Colour )
  113. {
  114.         _asm
  115.         {
  116.                 mov ah,  0x0C       ; specify apropriate function
  117.                 mov al,  [Colour]   ; put required colour in low byte
  118.                 mov cx,       [X]   ; X coordinate
  119.                 mov dx,       [Y]   ; Y coordinate
  120.                 mov bx, 0x01        
  121.                 int 10h             ; execute interrupt
  122.         }
  123. }
  124.  
  125. //------------------------------------------------------
  126. // PutASMPixel - Uses assembly code to place a pixel
  127. //         at X, Y directly to memory
  128. //------------------------------------------------------
  129.  
  130. void PutASMPixel( int X, int Y, unsigned char Colour )
  131. {
  132.       _asm
  133.       {
  134.       mov ax, 0xA000   ; point AX to video memory
  135.       mov es, ax       ; move segment pointer to ES
  136.                ; (actual pointer)
  137.       mov bx, [Y]
  138.       mov dx, bx       ; register to register is faster by 1 clock
  139.  
  140.       mov dh, dl       ; dx=y*256 + y
  141.       xor dl, dl       ; clear lower byte of DX
  142.  
  143.       shl bx, 6       ; bx=y*64
  144.       add bx, dx       ; bx=y*320
  145.  
  146.       add bx, [X]       ; bx=(y*320)+x
  147.       mov di, bx       ; move video pointer to correct place
  148.  
  149.       mov al, [Colour]
  150.       mov es:[di], al  ; move colour to memory
  151.       }
  152. }
  153.  
  154. void Intro()
  155. {
  156.     printf( "This program aims to demonstrate 3 ways of writing pixels to the screen.\n\n" );
  157.     printf( "1. Interrupt pixels - This routine uses Int. 10h to place a pixel (part ASM)\n" );
  158.     printf( "2. DMA pixels       - These pixels are placed on screen by writing directly\n");
  159.     printf( "                      to memory.\n" );
  160.     printf( "3. ASM pixels       - This PutPixel routine was written in assembler for speed.\n\n");
  161.     printf( "These routines loop 10 times each to demonstrate the speed differences.\n" );
  162.  
  163.     getch();
  164. }
  165.  
  166. void Outtro()
  167. {
  168.     printf( "Goodbye.....\n\n" );
  169.     printf( "Barny Mercer - 29/7/95 @ 12:39am\n" );
  170.     printf( "barny.mercer@zetnet.co.uk\n" );
  171.  
  172.     getch();
  173. }
  174.